package com.game.doudizhu.repository;

import com.game.doudizhu.model.Player;
import com.game.doudizhu.model.PlayerItem;
import com.game.doudizhu.model.ShopItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.query.Param;

import java.util.List;

@Repository
public interface PlayerItemRepository extends JpaRepository<PlayerItem, Long> {
    
    List<PlayerItem> findByPlayerId(Long playerId);
    
    @Query("SELECT pi FROM PlayerItem pi WHERE pi.player = :player AND pi.item.type = :type")
    List<PlayerItem> findByPlayerAndItemType(Player player, ShopItem.ItemType type);
    
    boolean existsByPlayerAndItem(Player player, ShopItem item);
    
    // 获取玩家未拥有的商品ID列表
    @Query("SELECT s.id FROM ShopItem s WHERE s.id NOT IN " +
           "(SELECT pi.item.id FROM PlayerItem pi WHERE pi.player.id = :playerId)")
    List<Long> findNotOwnedItemIds(Long playerId);
    
    // 获取玩家已装备的指定类型物品
    @Query("SELECT pi FROM PlayerItem pi WHERE pi.player.id = :playerId AND pi.item.type = :type AND pi.equipped = true")
    List<PlayerItem> findEquippedItemsByType(Long playerId, String type);

    @Modifying
    @Query("DELETE FROM PlayerItem pi WHERE pi.player.id = :playerId")
    void deleteByPlayerId(@Param("playerId") Long playerId);
} 